import app from '@/lib/app.js'

const guid = function () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    let r = Math.random() * 16 | 0
    let v = c === 'x' ? r : (r & 0x3 | 0x8)
    return v.toString(16)
  })
}

const notice = function (type, msg) {
  this.$Notice[type]({
    title: '提示',
    desc: msg,
    duration: 2.5
  })
}

const modal = function (type, msg, handleOk, handleCancel) {
  this.$Modal[type]({
    title: '提示',
    content: `<p>${msg}</p>`,
    onOk: handleOk,
    onCancel: handleCancel ? handleCancel : () => {}
  });
}

const tree2arr = function (data) {
  // 从树形结构转换为数组
  let arr = [];
  if (data instanceof Array) {
    data.map(item => {
      arr = arr.concat(this.tree2arr(item));
    });
  } else if (data instanceof Object) {
    arr.push(data);
    if (!data.children || (data.children && !data.children.length)) {
      return arr;
    } else {
      arr = arr.concat(this.tree2arr(data.children));
    }
  }
  return arr;
}

export default function () {
  return {
    data() {
      return {
        app: app,
        isLoadedDict: false
      }
    },
    methods: {
      guid,
      notice,
      modal,
      tree2arr
    }
  }
}
